#include <QFile>
#include <QWebSettings>
#include <QDebug>
#include <QDesktopWidget>
#include <QApplication>
#include "mainwindow.h"
#include "graphicshybridview.h"

MainWindow::MainWindow(QWidget *parent) :
		QMainWindow(parent),
		m_view(this)
{
	m_hybridView = new GraphicsHybridView(this,0,"@widget_identifier");

#if @panning_enabled
	// If panning is enabled, make sure the web view is resized when so required.
	connect(m_hybridView, SIGNAL(contentsSizeChanged(QSize)),
			this, SLOT(contentsSizeChanged(QSize)));
#endif

	m_hybridView->load(QUrl("@resource_file"));
	connect(m_hybridView, SIGNAL(contentsSizeChanged(QSize)),
			this, SLOT(contentsSizeChanged(QSize)));

#if @panning_enabled
	m_view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	m_view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	m_view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	m_view.setFrameStyle(QFrame::NoFrame);

	m_pannableView = new PannableView(Qt::Vertical | Qt::Horizontal);
	m_pannableView->setWidget(m_hybridView);

	m_scene.addItem(m_pannableView);
	m_view.setScene(&m_scene);
#else
	m_scene.addItem(m_hybridView);
#endif

	m_view.setScene(&m_scene);
	m_view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	m_view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setCentralWidget(&m_view);
	resizeEvent(0);
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
	QDesktopWidget *desktopWidget = QApplication::desktop();
	QRect clientRect;

	if (isFullScreen())
		clientRect = desktopWidget->screenGeometry();
	else
		clientRect = desktopWidget->availableGeometry();

	int width = clientRect.width();
	int height = clientRect.height();

#if @panning_enabled
	m_pannableView->setMinimumSize(width, height);
	m_pannableView->setPreferredSize(width, height);
	m_pannableView->setMaximumSize(width, height);
#else
	m_hybridView->resize(width, height);
#endif
}

void MainWindow::contentsSizeChanged(QSize newSize)
{
	m_hybridView->setMinimumSize(newSize.width(), newSize.height());
}
